package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author zhangqi
 * @email 562895852@qq.com
 * @date 2021-10-08 15:31:19
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取当前品牌关联的所有分类
     * @param params
     * @return
     */
    @GetMapping("/catelog/list")
    public R categoryBrandRelationList(@RequestParam Map<String, Object> params){
       // 根据品牌id查询关联关系
        List<CategoryBrandRelationEntity> list =  categoryBrandRelationService.queryPageByBrandId(params);
       return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存   新增关联关系
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        //由于关联表中存储了，品牌名称，分类名称，所以在新增的时候查出相关的名称进行保存
        /**
         * 注意：
         *   1.关联表有商品名称brandName,当商品修改了商品名称的时候 - 需要更新关联表中的商品名称
         *   2.关联表有分类的名称categoryName(name),当分类中修改了分类名称 - 需要更新关联表中的分类名称
         */
        //1.根据brandId查出商品信息
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        // categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
